package com.cms.admin.core.util;

import java.security.MessageDigest;
import java.util.UUID;
import java.util.logging.Logger;

/**
 * Created by YYY-Work on 2017/4/5.
 */
public class RequestUtil {

    private static Logger logger=Logger.getLogger(RequestUtil.class.getSimpleName());

    private static final String APP_ID="20000";

    private static String APP_VERSION = "3.3.1";

    private static String jsonHeaderParam = "appId=%1$s" + "&" +
            "version=%2$s" + "&" +
            "method=%3$s" + "&" +
            "timestamp=%4$s" + "&" +
            "uuId=%5$s" + "&" +
            "sign=%6$s" + "&" +
            "lang=%7$s" + "&fill=";

    public static String genHeader(String timeStamp, String method) {
        String header = "";
        String uuid=UUID.randomUUID().toString();
        try {
            // long number1 = Math.abs(UUID.fromString(uuid).getLeastSignificantBits());
            long number1 = Long.valueOf(timeStamp);
            int index1 = (int) (number1 % 10);
            String msg = APP_ID + APP_VERSION + method + timeStamp + uuid + getChallenge(index1);
            String sign = signMsg(msg);
            header = String.format(jsonHeaderParam, APP_ID, APP_VERSION, method, timeStamp, uuid, sign, "zh-hans");
        } catch (Exception e) {
        }

        return header;
    }

    public static String signMsg(String msg) {
        String sign = "";

        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(msg.getBytes());
            byte[] buffer = md5.digest();
            sign = bcd2Str(buffer);
        } catch (Exception e) {
        }

        return sign;
    }

    public static String getChallenge(int index) {
        return WORD_TABLE[index];
    }

    private static final String[] WORD_TABLE = {
            "de facto",
            "pectic",
            "decade",
            "brokerage",
            "panic",
            "dichloroethylene",
            "periodic",
            "frequency",
            "Fourier",
            "mixture"};

    private static String bcd2Str(byte[] bytes) {
        char temp[] = new char[bytes.length * 2], val;

        for (int i = 0; i < bytes.length; i++) {
            val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
            temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');

            val = (char) (bytes[i] & 0x0f);
            temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
        }

        return new String(temp);
    }

    public static String encryptMessage(String timeStamp, String header, String body) {
        String cryptedMessage = null;

        StringBuffer pading192String = new StringBuffer(header);
        for (int i = 0; i < 192 - header.length(); i++) {
            pading192String.append("0");
        }
        cryptedMessage = pading192String + body;
        return cryptedMessage;
    }

}
